package com.siyoumi.app.modules.app_book.admin;

import com.siyoumi.app.entity.BookItem;
import com.siyoumi.app.modules.app_book.service.SvcBookItem;
import com.siyoumi.app.modules.app_book.service.SvcBookStore;
import com.siyoumi.app.modules.app_book.vo.VaBookItem;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.exception.EnumSys;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import com.siyoumi.validator.XValidator;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/xadmin/app_book/app_book__item_hot__edit")
public class app_book__item_hot__edit
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("热门基地-编辑");

        Map<String, Object> data = new HashMap<>();
        data.put("bitem_type", input("type", ""));
        data.put("bitem_order", 0);
        data.put("bitem_name", "热门基地");
        if (isAdminEdit()) {
            BookItem entity = SvcBookItem.getApp().loadEntity(getID());

            HashMap<String, Object> dataAppend = new LinkedHashMap<>();
            dataAppend.put("id", entity.getKey());
            //
            //合并
            data = entity.toMap();
            data.putAll(dataAppend);
        }
        getR().setData("data", data);
        //商家列表
        setPageInfo("list_store", SvcBookStore.getBean().getList());

        return getR();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/save")
    public XReturn save(@Validated() VaBookItem vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);
        //
        if (XStr.isNullOrEmpty(vo.getBitem_pid())) {
            result.addError(XValidator.getErr("bitem_pid", "选择商家"));
        }
        {
            JoinWrapperPlus<BookItem> query = SvcBookItem.getBean().listQuery();
            query.eq("bitem_type", "hot")
                    .eq("bitem_pid", vo.getBitem_pid());
            if (isAdminEdit()) {
                query.ne("bitem_id", getID());
            }
            BookItem entity = SvcBookItem.getApp().first(query);
            if (entity != null) {
                result.addError(XValidator.getErr("bitem_pid", "商家已存在"));
            }
        }
        //
        XValidator.getResult(result);
        //
        if (isAdminEdit()) {
            vo.setBitem_id(getID());
        }

        BookItem entity = SvcBookItem.getBean().save(vo);
        XReturn r = EnumSys.OK.getR();
        r.setData("entity", entity);
        return r;
    }
}